package ru.ifmo.swing.genetics;

import org.uncommons.watchmaker.swing.EvolutionControl;
import ru.ifmo.swing.EvolutionControlWithConfigProvider;

import java.util.List;
import java.util.Map;

/**
 * @author avhaliullin
 */
public class ControlConfigParamsFetcher {
    private List<EvolutionControlWithConfigProvider> controls;

    public ControlConfigParamsFetcher(List<EvolutionControlWithConfigProvider> controls) {
        this.controls = controls;
    }

    private void shift(int depth, StringBuilder res) {
        for (int i = 0; i < depth; i++) {
            res.append("    ");
        }
    }

    private void wrap(EvolutionControlWithConfigProvider control, StringBuilder res, int depth) {
        Map<String, Object> map = control.getParamValues();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            shift(depth, res);
            res.append(entry.getKey()).append(" -> ");
            if (entry.getValue() instanceof EvolutionControl) {
                res.append(entry.getValue().getClass().getSimpleName());
            } else {
                res.append(String.valueOf(entry.getValue()));
            }
            res.append("\n");
            if (entry.getValue() instanceof EvolutionControlWithConfigProvider) {
                wrap((EvolutionControlWithConfigProvider) entry.getValue(), res, depth + 1);
            }
        }
    }

    public String fetchParams() {
        StringBuilder sb = new StringBuilder();
        for (EvolutionControlWithConfigProvider control : controls) {
            sb.append(control.getClass().getSimpleName()).append("\n");
            wrap(control, sb, 1);
            sb.append("\n");
        }
        return sb.toString();
    }
}
